package com.ihelp.rest.mis.platform.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ihelp.rest.mis.platform.dao.AdmdivisionDao;
import com.ihelp.rest.mis.pojo.Admdivision;
import com.ihelp.rest.utils.JsonCommon;

@Service
public class AdmdivisionService {
	@Autowired
	private AdmdivisionDao admdivisionDao;
	
	/**
	 * 查询省、市、县JSON 数据
	 * 
	 * @param parentcode
	 * @return
	 */
	public String findAdmdivision(String parentcode) {
		if (StringUtils.isBlank(parentcode)) {
			// 默认为省
			parentcode = "0";
		}
		return JSONArray.fromObject(
				this.admdivisionDao.queryAdmdivByParentCode(parentcode))
				.toString();
	}
	
	/**
	 * 查询行政区划Page 返回Json串
	 * @param queryCondition
	 * @param start
	 * @param pageSize
	 * @param sort
	 * @param order
	 * @return
	 */
	public String findAdmdivsPage(Admdivision queryCondition,int start,int pageSize,String sort, String order){
		return JsonCommon.getGridJson(this.admdivisionDao.queryAdmdivPage(queryCondition,start,pageSize,sort,order)).toString();
	}
	
	
	/**
	 * 得到行政区划树
	 * @param parentcode
	 * @return
	 */
	public String doGetAdmdiv(String parentcode){
		StringBuilder sb = new StringBuilder();
		sb.append("[");
		if(StringUtils.isBlank(parentcode)){
			List<Admdivision> rootResult = this.admdivisionDao.queryAdmdivByParentCode("0");
			sb.append("{");
			sb.append("\"id\":\"0\",");
			sb.append("\"text\":\"行政区划\"");
			if (null != rootResult && rootResult.size() > 0) {
				sb.append(",\"state\":\"closed\"");
			}
			sb.append("}");
		}else{
			List<Admdivision> result = this.admdivisionDao.queryAdmdivByParentCode(parentcode);
			for(Admdivision admdiv : result){
				boolean haveSub = false;
				List<Admdivision> subResult = this.admdivisionDao.queryAdmdivByParentCode(admdiv.getAdcode());
				if (null != subResult && subResult.size() > 0) {
					haveSub = true;
				}		
				sb.append("{");
				sb.append("\"id\":\"").append(admdiv.getAdcode()).append("\",");
				sb.append("\"text\":\"").append(admdiv.getAdname()).append("\"");
				if (haveSub) {
					sb.append(",\"state\":\"closed\"");
				}
				sb.append("}");
			}
		}
		sb.append("]");
		sb = new StringBuilder(sb.toString().replace("}{", "},{"));
		return sb.toString();
	}
	
	public Admdivision getAdmdivById(String id){
		return this.admdivisionDao.getAdmdivById(Long.valueOf(id));
	}
	
	public Admdivision getAdmdivByAdcode(String adcode){
		return this.admdivisionDao.getAdmdivByCode(adcode);
	}
	
	/**
	 * 保存行政区划
	 * @param admdiv
	 * @return
	 */
	public String saveAdmdivision(Admdivision admdiv){
		Map<String ,Object> result = new HashMap<String, Object>();
		result.put("success", false);
		try{
			//先检查区划编码规则
			int flag = 0;
			String adcode = admdiv.getAdcode();
			String parentcode = admdiv.getParentcode();
			if(parentcode.equals("0")){
				if(adcode.length()!=2)
					flag = 1;
			}else{
				if((adcode.length()-parentcode.length())!=2 && !adcode.substring(0, adcode.length()-1).equals(parentcode))
					flag = 1;
			}
			if(flag == 1)
				result.put("data", "区划编码格式不对");
			else{
				long l_count = this.admdivisionDao.validCode(admdiv);
				if(l_count>0){
					result.put("data", "此区划编码已存在！");
				}else{
					this.admdivisionDao.saveOrUpdate(admdiv);
					result.put("success", true);
					result.put("data", "保存成功");
				}	
			}
			
		}catch(Exception e){
			result.put("data", "保存失败"+e.getMessage());
		}
		return JSONObject.fromObject(result).toString() ;
	}
	
	/**
	 * 删除行政区划
	 * @param id
	 * @return
	 */
	public String delAdmdivision(String id){
		Map<String ,Object >  result = new HashMap<String, Object>();
		result.put("success", false);
		try{
			long lcount = this.admdivisionDao.getSubAdNum(id);
			if(lcount>0){
				result.put("data", "此行政区划下有子项,不能删除,请选删除子项！");
			}else{
				this.admdivisionDao.delete(Long.valueOf(id));
				
				result.put("success", true);
				result.put("data", "删除成功！");
			}	
		}catch(Exception e){
			result.put("data", "删除失败"+e.getMessage());
		}
		return JSONObject.fromObject(result).toString() ;
	}
	
}
